import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Enumeration;

@WebServlet("/f1")
public class ServletTest extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method=req.getMethod();
        String requestURL=req.getRequestURL().toString();
        String requestURI=req.getRequestURI();
        String contextPath=req.getContextPath();

        System.out.println("请求方法："+method);
        System.out.println("完整请求URL:"+requestURL);
        System.out.println("请求资源URI"+contextPath);

        Enumeration<String> headerNames=req.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String headerName=headerNames.nextElement();
            System.out.println("Header Name:"+headerName);
            String headerValue=req.getHeader(headerName);
            System.out.println("header Value:"+headerValue);
        }
        String userAgent=req.getHeader("User-Agent");
        System.out.println("本次请求的用户代理："+userAgent);
        String host=req.getRemoteHost();
        String address=req.getRemoteAddr();
        int clientPort=req.getRemotePort();
        String serverName=req.getServerName();
        int serverport=req.getRemotePort();
        String scheme=req.getScheme();
        System.out.println("获取发起请求的客户端主机名:"+host);
        System.out.println("获取客户端Ip地址:"+address);
        System.out.println("获取客户端使用的网络端口:"+clientPort);
        System.out.println("获取服务器的主机名或Ip地址:"+serverName);
        System.out.println("获取接收请求的服务器端口"+serverport);
        System.out.println("获取请求使用的协议"+scheme);
        String param=req.getParameter("name");
        String params= Arrays.toString(req.getParameterValues("hobby"));
        String queryString=req.getQueryString();
        System.out.println("获取到的单个参数："+param);
        System.out.println("获取到的多个同名参数"+params);
        System.out.println("获取请求URl中携带的参数(?后面的参数)"+queryString);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doDelete(req, resp);
    }
}
